/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
<template>
  <div class="list-model user-list-model">
    <div class="table-box">
      <el-table
        :data="list"
        size="mini"
        height="calc(100vh - 430px)"
        style="width: 100%;"
      >
        <el-table-column
          type="index"
          :label="$t('#')"
          width="50"
        ></el-table-column>
        <el-table-column
          prop="userName"
          :label="$t('User Name')"
        ></el-table-column>
        <el-table-column
          prop="roleName"
          label='角色'
          width="90"
        ></el-table-column>
        <el-table-column
          prop="queue"
          :label="$t('Queue')"
          width="90"
        ></el-table-column>
        <el-table-column
          prop="email"
          :label="$t('Email')"
          min-width="150"
        ></el-table-column>
        <el-table-column
          prop="phone"
          :label="$t('Phone')"
          width="140"
        >
          <template slot-scope="scope">
            <span>{{scope.row.phone | filterNull}}</span>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('State')"
          width="60"
        >
          <template slot-scope="scope">
            <span :class="scope.row.state?'act':'dop'">{{scope.row.state === 1 ? `${$t('Enable')}` : `${$t('Disable')}`}}</span>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('Create Time')"
          width="135"
        >
          <template slot-scope="scope">
            <span>{{scope.row.createTime | formatDate}}</span>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('Update Time')"
          width="135"
        >
          <template slot-scope="scope">
            <span>{{scope.row.updateTime | formatDate}}</span>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('Operation')"
          width="120"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-tooltip
              v-auth="['system:users:update']"
              :content="$t('Edit')"
              placement="top"
            >
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit-outline"
                @click="_edit(scope.row)"
                circle
              ></el-button>
            </el-tooltip>
            <el-tooltip
              v-auth="['system:users:delete']"
              :content="$t('Delete')"
              placement="top"
            >
              <el-button
                type="danger"
                size="mini"
                icon="el-icon-delete"
                circle
                @click="handleDel(scope.row)"
                :disabled="scope.row.id === 1"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
      title="删除用户"
      v-if="deleteUserDialog"
      :visible.sync="deleteUserDialog"
      width='auto'
      append-to-body
    >
      <m-popover
        ref="popover"
        @ok="_delOk"
        @close="_delClose"
      >
        <template slot="content">
          <div
            class="delete-user"
            style="padding:40px 25px;"
          >
            <i
              class="el-icon-warning"
              style="margin-right:8px;color:red;font-size: 18px;"
            ></i>
            <span
              class="right"
              style="font-size: 16px;"
              v-if="!isReal"
            >
              请确认是否要删除<span style="color:red;">{{ item.userName }}</span>，删除后不可恢复，请谨慎操作！
            </span>
            <span
              class="right"
              style="font-size: 16px;"
              v-else
            >
              用户组已存在关联用户，请先删除用户后，再次尝试！
            </span>
          </div>
        </template>
      </m-popover>
    </el-dialog>
  </div>
</template>
<script>
  import { mapActions } from 'vuex'
  import mPopover from '@/module/components/popup/popover'

  export default {
    name: 'user-list',
    data () {
      return {
        list: [],
        item: {},
        deleteUserDialog: false,
        isReal: false
      }
    },
    props: {
      userList: Array,
      pageNo: Number,
      pageSize: Number
    },
    methods: {
      ...mapActions('security', ['deleteUser', 'getAuthList', 'grantAuthorization', 'getResourceList']),
      _delOk () {
        this.deleteUser({
          id: this.item.id
        }).then(res => {
          this.$emit('on-update')
          this.$message.success(res.msg)
          this.deleteUserDialog = false
        }).catch(e => {
          this.$message.error(e.msg || '')
        })
      },
      _delClose () {
        this.deleteUserDialog = false
      },
      handleDel (row) {
        this.item = row
        this.deleteUserDialog = true
      },
      _edit (item) {
        this.$emit('on-edit', item)
      },
    },
    watch: {
      userList (a) {
        this.list = []
        setTimeout(() => {
          this.list = a
        })
      }
    },
    created () {
      this.list = this.userList
    },
    components: { mPopover }
  }
</script>

<style lang="scss" rel="stylesheet/scss">
.user-list-model {
  .user-list-poptip {
    min-width: 90px !important;
    .auth-select-box {
      a {
        font-size: 14px;
        height: 28px;
        line-height: 28px;
        display: block;
      }
    }
  }
}
.cli {
  margin-left: 12px;
  font-size: 14px;
  font-family: PingFang SC, PingFang SC-Regular;
  font-weight: 400;
  text-align: LEFT;
  color: #33317e;
  line-height: 20px;
  cursor: pointer;
}
.otherCli {
  margin-left: 12px;
  font-size: 14px;
  font-family: PingFang SC, PingFang SC-Regular;
  font-weight: 400;
  text-align: LEFT;
  color: #33317e;
  // background: #fff;
  line-height: 20px;
  border: 0;
  cursor: pointer;
}
</style>
<style lang="scss" scoped>
.act {
  color: #5ab92f;
}
.dop {
  color: #f56c6c;
}
</style>
